<template>
	<view class="the-page">
		<view class="card-item" v-for="(order, i) in orderList" :key="i">
			<!-- 订单头部 -->
			<uni-list-item :title="'订单ID：' + order.id" :rightText="`订单时间：${order.createTime}`" />
			<uni-list-item direction="column">
				<!-- 自定义 body -->
				<template v-slot:body>
					<view style="display: flex;flex-direction: row;justify-content: space-between;">
						<view>
							<uni-tag :text="orderStatus(order)" type="warning"></uni-tag>
						</view>
						<view style="margin-left: 10px;" v-if="order.status != 1">
							<uni-tag text="取消订单" type="error" @click="handleOrderCancel(order.id)"></uni-tag>
						</view>
					</view>
				</template>
			</uni-list-item>

			<!-- 商品列表 -->
			<view class="goods-item">
				<!-- 商品卡片 -->
				<uni-list-item v-for="item in order.orderItemList" :key="item.id" clickable
					@click="handle2OrderDetail(order.id)" :border="false" :thumb="item.goodsImg">
					<template v-slot:body>
						<uni-list-item direction="column" :border="false" :title="item.goodsName" :ellipsis="2"
							:note="item.goodsSpec">
							<template v-slot:footer>
								<view class="price-count">
									{{`${item.price} X ${item.count} = ${item.price * item.count}`}}
								</view>
							</template>
						</uni-list-item>
					</template>
				</uni-list-item>
			</view>

			<!-- 合计信息 -->
			<view class="amount">
				实付金额：
				<span style="color:red">¥{{ order.amount }}</span>
			</view>
		</view>
	</view>
</template>

<script>
	import * as orderAPI from '@/api/order.js';

	export default {
		name: 'orderList',
		data() {
			return {
				orderList: [], //订单列表
				//查询参数
				query: {
					pageNum: 1, //当前页码,页码从1开始计
					pageSize: 10000, //页码大小,这里设置所有订单，不分页处理了
					status: null //
				}
			};
		},

		//生命周期钩子函数
		// onLoad() {
		// 	this.getOrderList();
		// },
		onShow() {
			this.getOrderList();
		},

		methods: {
			//获取订单列表
			getOrderList() {
				orderAPI.getMyList(this.query).then(result => {
					console.log(result); //调式输出
					this.orderList = []; //清空数据
					this.orderList = result.data.list;
				});
			},

			//订单状态格式转换  订单状态( 0初始 1取消 2完成)
			orderStatus(order) {
				let status = "未知";
				switch (order.status) {
					case 0:
						status = "待支付";
						break;
					case 1:
						status = "已取消";
						break;
					case 2:
						status = "已支付";
						break;
				}
				return status;
			},

			// 取消订单 状态(0待支付/1已取消/2已支付)
			handleOrderCancel(id) {
				orderAPI.updateStatus(id, 1).then(() => {
					this.getOrderList(); //刷新列表数据
				});
			},

			// 触发导航到订单详情
			handle2OrderDetail(id) {
				uni.navigateTo({
					url: '/pages/order/orderDetail?orderId=' + id
				});
			},

		}
	};
</script>

<style lang="scss" scoped="scoped">
	.the-page {
		background-color: $uni-bg-color;
	}

	.card-item {
		margin: 5px;
		padding: 5px;
		background-color: $uni-bg-color-grey;
		border-radius: 10px;
		font-size: 16px;
	}

	.card-header {
		font-size: 14px;
		font-weight: bolder;
	}

	.price-count {
		padding-top: 10px;
		font-size: 14px;
		color: $uni-text-color;
		text-align: right;
	}

	// 样式穿透
	::v-deep .uni-list--base {
		width: 80px;
		height: 80px;
	}

	.amount {
		margin-top: 5px;
		padding: 5px 5px;
		text-align: right;
		color: $uni-text-color;
		font-weight: bold;
		background-color: $uni-bg-color;
		font-size: 14px;
	}
</style>